/*
    Copyright Nicolas Ronvel 2007
	gulix33xp@yahoo.fr

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

using System;
using System.Drawing;

using SdlDotNet.Graphics;
using Sdl = SdlDotNet.Graphics;
using SdlDotNet.Core;
using SdlDotNet.Input;

using BlindShark.GameElements;

using Gulix.Audio;
using BlindShark.Widgets;

namespace BlindShark.GameStates
{
	public class GS_Answering : GameState
	{
		// Surfaces
		Surface _sfFrame;
		Surface _sfAvatar;
		
		// Labels
		Label _lblTitle;
		Label _lblPlayer;
		Label _lblScore;
		Label _lblCountdown;
		
		// Buttons
		Button _btnValidate;
		
		// Font
		string _sFont;
		
		// Countdown
		int _iCountdown;
				
		// TextBox to Answer
		TextBox _txtAnswer;
		
		// Elements Location
		Rectangle _rctFrame;
		Rectangle _rctAvatar;
		
		TimeEvent _teAutoValidation = null;
		
		GS_InGame _gsSource;
		
		public GS_Answering(GS_InGame game)
		{
			_gsSource = game;
			_sFont = "fonts/Fertigo_17b.otf";
			
			// Initializing the button
			_btnValidate = new Button("gfx/buttons1.jpg", true);
			_btnValidate.Click += new Button.ClickEvent(ButtonValidate);
			_btnValidate.Font = _sFont;
			_btnValidate.Text = LanguageManager.Get("ANS_Validate");
			_btnValidate.TextSize = 20;
			_btnValidate.TextColor = Color.Orange;
			
			// Initializing the labels
			// Title of the frame
			_lblTitle = new Label(LanguageManager.Get("ANS_Answer"), 36, Color.OrangeRed, TextAlignment.TopMiddle);
			_lblTitle.Underline = true;
			_lblTitle.Location = new Point(0,15);
			// Player's name
			_lblPlayer = new Label(PlayerManager.GetName(PlayerManager.Answering), 24, Color.Brown, TextAlignment.TopMiddle);
			// Player's Score
			_lblScore = new Label(LanguageManager.Get("ANS_Score") + " : " + PlayerManager.GetScore(PlayerManager.Answering).ToString() + " " + LanguageManager.Get("ANS_Points"),
			                      20, Color.Brown, TextAlignment.TopMiddle);
			// Countdown
			_lblCountdown = null;
			if (GameManager.AnsweringTimeLimit>0)
			{
				_iCountdown = GameManager.AnsweringTimeLimit;
				_teAutoValidation = new TimeEvent(_iCountdown, 1000, new TimeEvent.Call(CountdownTick));
				ClockManager.AddTimeEvent(_teAutoValidation);
				_lblCountdown = new Label(GameManager.AnsweringTimeLimit.ToString(), 80, Color.Olive , TextAlignment.CenterMiddle);
			}
			
			// Initializing the TextBox
			_txtAnswer = new TextBox();
			_txtAnswer.OnValidation += new TextBox.Validation(ButtonValidate);
			_txtAnswer.FontFile = _sFont;
			_txtAnswer.Focused = true;
			
			
			// Keyboard events
			Events.Remove();
		}
		
		public void Draw()
		{
			// Drawing the Frame
			ScreenManager.MainScreen.Blit(_sfFrame, _rctFrame.Location);
			
			// Drawing the TextBox
			ScreenManager.MainScreen.Blit(_txtAnswer.Surface, _txtAnswer.Location);
			
			// Drawing the Button
			ScreenManager.MainScreen.Blit(_btnValidate.Surface, _btnValidate.Location);
			
			// Drawing the CountDown
			if (_lblCountdown != null)
				ScreenManager.MainScreen.Blit(_lblCountdown.Surface, _lblCountdown.Location);
		}
		
		
		public void CleanUp()
		{
			_txtAnswer.CleanUp();
			_btnValidate.CleanUp();
			
			_lblTitle.CleanUp();
			_lblPlayer.CleanUp();
			_lblScore.CleanUp();
			if (_lblCountdown != null)
				_lblCountdown.CleanUp();
			
			Events.Remove();
			Keyboard.KeyRepeat = false;
		}
		
		public void HandleEvents()
		{
			while (Events.Poll())
			{
				_txtAnswer.HandleEvents();
				_btnValidate.HandleEvents();
			}
		}
		
		public void Update()
		{
			
		}
		
		public void Init()
		{
			// Initializing the frame
			_sfFrame = new Surface(SurfaceManager.GetSurface("gfx/background_infoWindow.png").Size);
			_sfFrame.Blit(SurfaceManager.GetSurface("gfx/background_infoWindow.png"));
			_rctFrame = new Rectangle(new Point(150, 50), _sfFrame.Size);
			
			// Initializing the avatar
			_rctAvatar = new Rectangle(0,0,200,200);
			_sfAvatar = new Surface(_rctAvatar.Size);
			_sfAvatar.Blit(SurfaceManager.GetSurface("avatars/"+PlayerManager.GetAvatar(PlayerManager.Answering)).CreateStretchedSurface(_rctAvatar.Size));
			_rctAvatar.Location = new Point(20, 70);
			_sfFrame.Blit(_sfAvatar, _rctAvatar.Location);
			
			// Initializing the labels
			// Title
			_lblTitle.Size = new Size(_rctFrame.Width, 0);
			_lblTitle.AutoHeight = true;
			_lblTitle.Init();
			_sfFrame.Blit(_lblTitle.Surface, _lblTitle.Location);
			// Player's Name
			_lblPlayer.AutoHeight = true;
			_lblPlayer.Width = _rctFrame.Width - _rctAvatar.Width - 40;
			_lblPlayer.Location = new Point(_rctAvatar.Right, _rctAvatar.Y + 20);
			_lblPlayer.Init();
			_sfFrame.Blit(_lblPlayer.Surface, _lblPlayer.Location);
			// Player's Score
			_lblScore.AutoHeight = true;
			_lblScore.Width = _lblPlayer.Width;
			_lblScore.Location = new Point(_rctAvatar.Right, _lblPlayer.Bottom + 10);
			_lblScore.Init();
			_sfFrame.Blit(_lblScore.Surface, _lblScore.Location);
			// Countdown
			if (_lblCountdown != null)
			{
				_lblCountdown.Size = new Size(200, 200);
				_lblCountdown.X = _rctFrame.X + 20;
				_lblCountdown.Y = _rctFrame.Bottom - 20 - _lblCountdown.Height;
				_lblCountdown.Init();
			}
			
			// Initializing the Textbox
			_txtAnswer.Location = new Point(_rctFrame.X + 20, _rctFrame.Y + _rctAvatar.Bottom + 30);
			_txtAnswer.Size = new Size(_rctFrame.Width - 40, 30);
			_txtAnswer.Init();
			
			// Initializing the Button
			_btnValidate.Init();
			_btnValidate.X = _txtAnswer.Right - _btnValidate.Width;
			_btnValidate.Y = _txtAnswer.Bottom + 10;
		}
		
		public void Validate()
		{
			// Good Answer
			if (GameManager.CurrentTrack.IsCorrect(_txtAnswer.Text, GameManager.AnswerAccuracy))
			{
				int score = 0;
				if (!GameManager.ChallengeMode)
					score = GameManager.GainOnGoodAnswer;
				// Challenge mode points
				else if (PlayerManager.Answering==GameManager.CurrentTrackOwner)
					score = GameManager.GainOwnTrack;
				else
					score = GameManager.GainOtherTrack;
				
				// Changing the player score
				PlayerManager.SetScore(PlayerManager.Answering,
				                       PlayerManager.GetScore(PlayerManager.Answering) + score);
				_gsSource.InitPlayer(PlayerManager.Answering);
				
				GameStateManager.ChangeState(new GS_TrackResult(TrackResultType.GoodAnswer, score));
			} else {
				// Bad Answer
				int score = 0;
				if (!GameManager.ChallengeMode)
					score = GameManager.LossOnBadAnswer;
				// Challenge mode points
				else if (PlayerManager.Answering==GameManager.CurrentTrackOwner)
					score = GameManager.LossOwnTrack;
				else
					score = GameManager.LossOtherTrack;
				
				// Changing the player score
				PlayerManager.SetScore(PlayerManager.Answering, 
				                       PlayerManager.GetScore(PlayerManager.Answering) - score);
				_gsSource.InitPlayer(PlayerManager.Answering);
				PlayerManager.SetState(PlayerManager.Answering, PlayerState.outGame);
				
				GameStateManager.ChangeState(new GS_TrackResult(TrackResultType.BadAnswer, score));
			}
		}
		
		private void CountdownTick()
		{
			// Decreasing the countdown
			_iCountdown--;
			if (_iCountdown<=0)
				Validate();
			_lblCountdown.Text = _iCountdown.ToString();
			_lblCountdown.Init();
		}
		
		private void ButtonValidate()
		{
			if (_teAutoValidation!=null)
				ClockManager.Remove(_teAutoValidation);
			_teAutoValidation = null;
			Validate();
		}
	}
}
